package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelDateInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * 小说一级页面适配器
 * Created by lanouhn on 16/9/5.
 */
public class MoreNovelAdapter extends BaseAdapter {

    private Context mContext;
    private List<MoreNovelInfo.DataBean.CatwordIdBean> lists;
    private MoreNovelDateInfo mMoreNovelDateInfo;

    public MoreNovelAdapter(Context mContext, List<MoreNovelInfo.DataBean.CatwordIdBean> lists, MoreNovelDateInfo mMoreNovelDateInfo) {
        this.mContext = mContext;
        this.lists = lists;
        this.mMoreNovelDateInfo = mMoreNovelDateInfo;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_more_novel, null);
            holder.title = (TextView) convertView.findViewById(R.id.more_novel_title);
            holder.content = (TextView) convertView.findViewById(R.id.more_novel_content);
            holder.update = (TextView) convertView.findViewById(R.id.more_novel_update);
            holder.pic = (ImageView) convertView.findViewById(R.id.more_novel_pic);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(lists.get(position).getName());
        holder.content.setText(lists.get(position).getDesc());
        Picasso.with(mContext).load(lists.get(position).getPic_url()).into(holder.pic);
        /**
         * 获取时间
         */
        Long data = mMoreNovelDateInfo.getData().get(position).getUpdated() * 1000L;
        Date date = new Date(data);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        String time = format.format(date);
        holder.update.setText(time + "更新");
        return convertView;
    }

    private class ViewHolder {
        private ImageView pic;
        private TextView title, content, update;

    }
}
